#include <stdio.h>
#include "ellipsincludes.h"
#include "film.h"
#include "AutoE.h"
#include "ManEllips.h"
#include "MainMenu.h"
#include "ChangeSettings.h"

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 	4

void setDefaults(ellipspar_t *ellipsparams);
//int changeSettings(ellipspar_t *ellipsparams);
int allocatevalues(ellipspar_t *ellipsp);

int main (int argc, const char * argv[]) {
    // insert code here...
	// Outline
	// Prompt user for function
	//	1. Input Psi and Delta 
	//	2. Manually step ellipsometer
	//	3. Run ellipsometer in automode
	///////////////////////////////////
	
	
	// use ncurses to do screenhandling
	// initialise ncurses
	static WINDOW *mainwnd;
	 ellipspar_t *ellipsparams; // contains the ellipsometer settings
	printf("allocating params\n");
	//allocatevalues(ellipsparams);
	if (( ellipsparams = (ellipspar_t *) malloc( sizeof(ellipspar_t) ) ) == NULL) {
		printf("ERROR ALLOCATING ellipsparams\n");
		exit;
	}
	for (int i; i<STRUCT_ENTRIES; i++) {
		ellipsparams->ellipspar_names[i]=malloc(50*sizeof(char));
	} // allocates space for arrays of names
	
	
	printf("params allocated\n");
	printf("setting default values\n");
	setDefaults(ellipsparams);
	printf("Defaults set\n");
	//screen_init(mainwnd);				/* start the curses mode */
	mainwnd = initscr();
	
	//   noecho();
	cbreak();
	//   nodelay(mainwnd, TRUE);
	wprintw(mainwnd,"window init complete\n");
	while(1)
	{
	wrefresh(mainwnd); 
	int choice=MainMenu(mainwnd); // get menu selection 

		// now have menu choice can operate on it
		switch (choice)
		{
			case 1:
				//"1. Auto run",
				AutoE(mainwnd,ellipsparams);
				//endwin();
				//return 0;
				
				break;
			case 2:
				//"2. Manually acquire data",
				manE(mainwnd,ellipsparams);
				
				//endwin();
				//return 0;
				break;
				
			case 3:
				//"3. Input Psi and Delta values",
				//clear();
				getpsidelta_solve(mainwnd,ellipsparams);
				break;
			case 4:
				// input n and k o/p psi and delta
				nkToPsiDelta(mainwnd,ellipsparams);
				break;
			case 5:
				// View/change ellipsometer settings
				changeSettings(mainwnd,ellipsparams);
				break;
			case 6:
				//"6. Exit"
				endwin();
				return 0;
		}
	}	
	
	
	//endwin();
	
		
    //printf("Hello, World!\n");
    return 0;
}


void setDefaults(ellipspar_t *ellipsparams) // set default values
{
	
	ellipsparams->nvalues=STRUCT_ENTRIES; // record of size of arrays.
	
	printf("nvalues\n");
	strcpy(ellipsparams->ellipspar_names[0], "Angle (degrees)");
	//ellipsparams->ellipspar_names[0][0]="Angle (degrees)";
	printf("name:angle");
	ellipsparams->ellipspar_values[0]=70.;
	printf("value:angle\n");
	strcpy(ellipsparams->ellipspar_names[1],"Re{Substrate RI}");
    ellipsparams->ellipspar_values[1]=3.85;
	strcpy(ellipsparams->ellipspar_names[2],"Im{Substrate RI}");
	ellipsparams->ellipspar_values[2]=-0.02; // substrate refractive index default to silicon value
	strcpy(ellipsparams->ellipspar_names[3],"Re{Capping RI}");
	ellipsparams->ellipspar_values[3]=1.0;
	strcpy(ellipsparams->ellipspar_names[4],"Im{Capping RI}");
	ellipsparams->ellipspar_values[4]=0.;      // capping medium refractive index default to air value
	strcpy(ellipsparams->ellipspar_names[5],"Wavelength(A)");
    ellipsparams->ellipspar_values[5]=6328.2;                  // laser wavelength in Angstromms
}
